package com.apartment.controller;


import com.apartment.auto.JSONAuthentication;
import com.apartment.entity.ApUser;
import com.apartment.mapper.ApUserMapper;
import com.apartment.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author apartment
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/ap-user")
public class ApUserController extends JSONAuthentication {
    @Autowired
    ApUserMapper apUserMapper;
    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String username, @RequestParam("password") String password1, @RequestParam("role") String role) throws  Exception{
        ApUser anUser=new ApUser();
        anUser.setUsername(username);
        anUser.setPassword(password1);
        anUser.setRole(role);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("username",anUser.getUsername());
        ApUser a= apUserMapper.selectOne(wrapper);
        if(a!=null){
            Result result= Result.error();
            result.setMessage("该用户已存在");
            this.WriteJSON(request, response, result);
        }else {
            String password= new BCryptPasswordEncoder().encode(anUser.getPassword());
            anUser.setPassword(password);
            int i=apUserMapper.insert(anUser);
            if(i>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除用户")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=apUserMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改密码")
    @PostMapping("/updPassword")
    public void updUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("password") String password,@RequestParam("rpassword") String rpassword,@RequestParam("id") int id )throws Exception{
        ApUser shUser=apUserMapper.selectById(id);
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        if(encoder.matches(password,shUser.getPassword())){
            String newPassword=new BCryptPasswordEncoder().encode(rpassword);
            shUser.setPassword(newPassword);
            if(apUserMapper.updateById(shUser)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error();
            error.setMessage("密码不正确");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "添加资料")
    @PostMapping("/addMenus")
    public void addMenus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("phone") String phone, @RequestParam("sf") String sf) throws  Exception{
        ApUser anUser=apUserMapper.selectById(id);
        anUser.setPhone(phone);
        anUser.setSf(sf);
        anUser.setName(name);
            if(apUserMapper.updateById(anUser)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
    }
    @ApiOperation(value = "修改资料")
    @PostMapping("/updMenus")
    public void updMenus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("phone") String phone, @RequestParam("sf") String sf) throws  Exception{
        ApUser anUser=apUserMapper.selectById(id);
        anUser.setPhone(phone);
        anUser.setSf(sf);
        anUser.setName(name);
        if(apUserMapper.updateById(anUser)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户名查找用户")
    @PostMapping("/findByUserName")
    public void findByUserName(HttpServletRequest request, HttpServletResponse response,@RequestParam("username") String  username,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("username",username);
        Page<ApUser> shUserPage=new Page<>(current,size);
        Page<ApUser> shUserPages=apUserMapper.selectPage(shUserPage,wrapper);
        List<ApUser> records = shUserPages.getRecords();
        int total=apUserMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无该名字用户");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过真实姓名查找用户")
    @PostMapping("/findByName")
    public void findByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String  name,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<ApUser> shUserPage=new Page<>(current,size);
        Page<ApUser> shUserPages=apUserMapper.selectPage(shUserPage,wrapper);
        List<ApUser> records = shUserPages.getRecords();
        int total=apUserMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无该名字用户");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有用户")
    @PostMapping("/findAllUser")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<ApUser> shUserPage=new Page<>(current,size);
        Page<ApUser> shUserPages=apUserMapper.selectPage(shUserPage,null);
        List<ApUser> records = shUserPages.getRecords();
        int total=apUserMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户id查找用户")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
        ApUser apUser = apUserMapper.selectById(id);
        if(apUser!=null){
            Result ok = Result.ok().data("user",apUser);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无该名字用户");
            this.WriteJSON(request, response, error);
        }
    }
}

